package com.etop.learnapp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.etop.learnapp.MyItemClickListener;
import com.etop.learnapp.R;

import java.util.ArrayList;


public class KeyAdapter extends RecyclerView.Adapter<KeyAdapter.ViewHolder> {
    private ArrayList<String> mDataset;
    private MyItemClickListener mItemClickListener;
    private  int[] resIDS = new int[46];
    public  class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        public TextView mTextView;
        public ImageView mImageView;
        private MyItemClickListener mListener;
        public ViewHolder(View itemView,MyItemClickListener mItemClickListener)
        {
            super(itemView);
            mListener = mItemClickListener;
            mTextView = (TextView) itemView.findViewById(R.id.tv_item);
            mImageView = (ImageView) itemView.findViewById(R.id.iv_item);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(mListener!=null)
            {
                mListener.onItemClick(view,getLayoutPosition());
            }
        }
    }
    public KeyAdapter(ArrayList<String> mDataset,int[] resIDs)
    {
        super();
        this.resIDS = resIDs;
        this.mDataset = mDataset;
    }
    @Override
    public KeyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_view, parent, false);
        return   new ViewHolder(v,mItemClickListener);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset.get(position));
        holder.mImageView.setImageResource(resIDS[position%27]);
    }

    public void setOnItemClickListener(MyItemClickListener listener){
        this.mItemClickListener = listener;
    }


    @Override
    public int getItemCount() {
        return mDataset.size();
    }

}

